package com.canbot.u05.activity.shoppingGuide.codeMarketing;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.bean.codeMarketing.CodeMarketingInfo;
import com.canbot.u05.bean.guide.MapVoiceAndEyes;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CodeTimeActivity extends BaseCodeFinishActivity {

        @BindView(R.id.code_iv_back)
        Button codeIvBack;

        @BindView(R.id.code_iv_ok)
        Button codeIvOk;

        @BindView(R.id.code_time10_iv)
        ImageView codeTime10Iv;

        @BindView(R.id.code_time30_iv)
        ImageView codeTime30Iv;

        @BindView(R.id.code_time60_iv)
        ImageView codeTime60Iv;

        private View currentView;

        private int currentPosition = -1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_code_time);
                ButterKnife.bind(this);
                initView();
                MapVoiceAndEyes.CODE_MARKETING[(int) (Math.random() * 2) + 6].playMapVoiceAndEyes();
        }

        private void initView() {
                CodeMarketingInfo codeMarketingSave = CodeDataHelper.getInstance().getCodeMarketingSave();
                if (codeMarketingSave != null) {
                        long interval = codeMarketingSave.getInterval();
                        setSelectView((int) (interval / 30 / 1000));
                }
                else {
                        setSelectView(0);
                }
        }

        private void setSelectView(int i) {
                if (currentPosition == i) {
                        return;
                }
                currentPosition = i;
                if (currentView != null) {
                        currentView.setSelected(false);
                }
                switch (i) {
                        case 0:
                                CodeDataHelper.getInstance().getCodeMarketingInfo().setInterval(10 * 1000);
                                currentView = codeTime10Iv;
                                codeTime10Iv.setSelected(true);
                                break;
                        case 1:
                                CodeDataHelper.getInstance().getCodeMarketingInfo().setInterval(30 * 1000);
                                currentView = codeTime30Iv;
                                codeTime30Iv.setSelected(true);
                                break;
                        case 2:
                                CodeDataHelper.getInstance().getCodeMarketingInfo().setInterval(60 * 1000);
                                currentView = codeTime60Iv;
                                codeTime60Iv.setSelected(true);
                                break;
                        default:
                                setSelectView(0);
                }
        }

        @OnClick({R.id.code_iv_back, R.id.code_iv_ok, R.id.code_time10_iv, R.id.code_time30_iv, R.id.code_time60_iv})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.code_iv_back:
                                finish();
                                break;
                        case R.id.code_iv_ok:
                                int type = CodeDataHelper.getInstance().getCodeMarketingInfo().getType();
                                if (CodeMarketingInfo.TYPE_FREEDOM == type) {
                                        startOtherActivity(CodeFreePreviewActivity.class, false);
                                }
                                else {
                                        startOtherActivity(CodePreviewActivity.class, false);
                                }
                                break;
                        case R.id.code_time10_iv:
                                setSelectView(0);
                                break;
                        case R.id.code_time30_iv:
                                setSelectView(1);
                                break;
                        case R.id.code_time60_iv:
                                setSelectView(2);
                                break;
                }
        }
}
